﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Core.OrderModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;
using System.Reflection;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Order")]

    public class OrderController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;
        SetInhibition setDetailInhibition;
        GetInhibition getDetailInhibition;

        SetInhibitionMaker inhibitionMaker = new SetOrderInhibitionMaker( new StateInhibitionMaker());
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        SetInhibitionMaker detailInhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getDetailInhibitionMaker = new GetInhibitionMaker();
        public OrderController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
            setDetailInhibition = detailInhibitionMaker.Make();
            getDetailInhibition = getDetailInhibitionMaker.Make();
        }

        OrderFetcher fetcher = new OrderFetcher();
        OrderDetailFetcher dfetcher = new OrderDetailFetcher();
        OrderTypeFetcher tfetcher = new OrderTypeFetcher();

        OrderManager manager = OrderManager.CreateInstance();
        OrderDetailManager dmanager = OrderDetailManager.CreateInstance();
        OrderTypeManager tmanager = new OrderTypeManager();

        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return  NormalControllerHelper.List(fetcher, "SAM_ORDER_LIST", RestainConverter.FromRequestArgs(paramModels),null, getInhibition);
        }

        [Route("Export")]
        [HttpGet]
        public ResponseModel Export([FromUri]RequestArgs paramModels)
        {
            ExpFetcher<OrderExp> fetcher = new ExpFetcher<OrderExp>();
            return NormalControllerHelper.Export(fetcher, "SAM_ORDER_LIST", RestainConverter.FromRequestArgs(paramModels));
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return NormalControllerHelper.Get(fetcher, "SAM_ORDER_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel Set(OrderModel data)
        {
            return NormalControllerHelper.Set(fetcher, "SAM_ORDER_SET", data, manager, setInhibition, getInhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(OrderModel data)
        {
            return NormalControllerHelper.Remove("SAM_ORDER_DEL", data, manager);
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("SAM_ORDER_DEL", id, manager);
        }

        [Route("ListDetails")]
        [HttpGet]
        public ResponseModel ListDetails(long orderid)
        {
            return NormalControllerHelper.ListDetail(dfetcher, "SAM_ORDER_LIST", orderid, new GetInhibition());
        }
        [Route("ListByCustomer")]
        [HttpGet]
        public ResponseModel ListByCustomer(long customer)
        {
            Restrain[] restrains = new Restrain[] { Restrain.Eq("CustomerId", customer), Restrain.Eq("State", Definition.STATE_APPROVED) };
            return NormalControllerHelper.List(new VOrderDetailFetcher(), "SAM_ORDER_LIST", restrains,null, new GetInhibition());
        }
        [Route("SetDetails")]
        [HttpPost]
        public ResponseModel SetDetails([FromBody] RequestModel<OrderDetailModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].OrderId != requestData.MasterId)
                {
                    requestData.Data[i].OrderId = requestData.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(dfetcher, dfetcher, "SAM_ORDER_SET", requestData, dmanager, setDetailInhibition);
        }

        [Route("ListType")]
        [HttpGet]
        public ResponseModel ListType([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(tfetcher, "SAM_ORDER_LIST", paramModels, new GetInhibition());
        }
        [Route("ListUnDelivery")]
        [HttpGet]
        public ResponseModel ListUnDelivery([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(new VOrderDetailFetcher(), "SAM_ORDER_LIST", paramModels, new GetInhibition());
        }
        [Route("GetType")]
        [HttpGet]
        public ResponseModel GetType(long id)
        {
            return NormalControllerHelper.Get(tfetcher, "SAM_ORDER_LIST", id, new GetInhibition());
        }

        [Route("SetType")]
        [HttpPost]
        public ResponseModel SetType(OrderTypeModel data)
        {
            return NormalControllerHelper.Set(tfetcher, "SAM_ORDER_SET", data, tmanager, new StateInhibitionMaker().Make(), new GetInhibition());
        }

        [Route("RemoveType")]
        [HttpPost]
        public ResponseModel RemoveType(OrderTypeModel data)
        {
            return NormalControllerHelper.Remove("SAM_ORDER_DEL", data, tmanager);
        }
        [Route("RemoveWithDetail")]
        [HttpGet]
        public ResponseModel RemoveWithDetail([FromUri]long id)
        {
            return NormalControllerHelper.RemoveWithDetail(dfetcher, "SAM_ORDER_DEL", id, manager, dmanager);
        }
        [Route("RemoveTypeById")]
        [HttpGet]
        public ResponseModel RemoveTypeById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("SAM_ORDER_DEL", id, tmanager);
        }
        [Route("Approve")]
        [HttpGet]
        public ResponseModel Approve([FromUri]long id)
        {
            return NormalControllerHelper.Approved("SAM_ORDER_APP", id, manager);
        }
        [Route("Revert")]
        [HttpGet]
        public ResponseModel Revert([FromUri]long id)
        {
            return NormalControllerHelper.Revert("SAM_ORDER_APP", id, manager);
        }
        [Route("Submit")]
        [HttpGet]
        public ResponseModel Submit([FromUri]long id)
        {
            return NormalControllerHelper.Submit("SAM_ORDER_SET", id, manager);
        }
        [Route("Recall")]
        [HttpGet]
        public ResponseModel Recall([FromUri]long id)
        {
            return NormalControllerHelper.Recall("SAM_ORDER_SET", id, manager);
        }
        [Route("Reject")]
        [HttpGet]
        public ResponseModel Reject([FromUri]long id)
        {
            return NormalControllerHelper.Reject("SAM_ORDER_APP", id, manager);
        }
        [Route("Comfirm")]
        [HttpGet]
        public ResponseModel Comfirm([FromUri]long id)
        {
            return NormalControllerHelper.Comfirm("SAM_ORDER_CFM", id, manager);
        }
    }
}
